/**
 * lsjs.dom
 * lsjs.css
 * 
 * @author asuslx
*/

lsjs.namespace('lsjs.dom');
lsjs.namespace('lsjs.css');

lsjs.dom.create = function(tagName, attrs, parentNode) { 
	
	var element = document.createElement(tagName);
	
	if(lsjs.isObject(parentNode)) {
		if((tagName == 'option') && lsjs.isIE()) {			
			parentNode.add(element);
		} else {			
			parentNode.appendChild(element);
		}	
	};	
	
	if(lsjs.isArray(attrs) || lsjs.isObject(attrs)) {		
		for (var attrName in attrs) {
			switch(attrName) {
			
				case 'id' :
				case 'Id' :
				case 'ID' :						
					element.id = attrs[attrName];
					break;	
				
				case 'value' :
				case 'Value' :
					element.value = attrs[attrName];
					break;
			
				case 'type' :
				case 'Type'	:
					element.type = attrs[attrName];
					break;	
				
				case 'name' :
				case 'Name' :
					element.name = attrs[attrName];
					break;
				
				case 'click' :
				case 'onclick' :
				case 'onClick' :	
					element.onclick = attrs[attrName];
					break;
			    
				case 'change' :
				case 'onchange' :
				case 'onChange' :	
					element.onchange = attrs[attrName];
					break;
			    
			    
				case 'checked' :
				case 'Checked' :
					element.checked = attrs[attrName];
					break;	                
                										
				case 'class' :
				case 'className' :
					element.className = attrs[attrName];
					break;
				
				case 'html':
				case 'innerHTML':
					if(tagName == 'option' && lsjs.isIE())
						element.text = attrs[attrName];
					else	
						element.innerHTML = attrs[attrName];
					break;	
			
				case 'text' :
					if(lsjs.isIE())
						element.text = attrs[attrName];
					else
						element.innerHTML = attrs[attrName];
					break;	
				default : 
					element.setAttribute(attrName, attrs[attrName]);
			}
		}	
	}	
	
	return element;	
};

lsjs.dom.hide = function(element) {
	element.style.display = 'none';
};

lsjs.dom.show = function(element) {
	element.style.display = '';
};

lsjs.dom.hidden = function(element) {
	return (element.style.display == 'none');
};

lsjs.dom.insertAfter = function(newElement, targetElement) {
	var parent = targetElement.parentNode;
	if(parent.lastchild == targetElement) {
		parent.appendChild(newElement);
	} else {
		parent.insertBefore(newElement, targetElement.nextSibling);
	}
};


lsjs.css.exists = function(element, nameOfClass) {
    if (lsjs.isString(element)) { element = document.getElementById(element); }

    if (element.className == '') {
        return false;
    } else {
        return new RegExp('\\b' + nameOfClass + '\\b').test(element.className);
    }
};

lsjs.css.add = function(element, nameOfClass) {
    if (lsjs.isString(element)) { element = document.getElementById(element); }

    if (!lsjs.css.exists(element, nameOfClass)) {
        element.className += (element.className ? ' ' : '') + nameOfClass;
        return true;
    } else {
        return false;
    }
};

lsjs.css.remove = function(element, nameOfClass) {
    if (lsjs.isString(element)) { element = document.getElementById(element); }

    if (lsjs.css.exists(element, nameOfClass)) {
        element.className = element.className.replace((element.className.indexOf(' ' + nameOfClass) >= 0 ? ' ' + nameOfClass : nameOfClass),'');
        return true;
    } else {
        return false;
    }
};

lsjs.css.replace = function(element, class1, class2) {
    if (lsjs.isString(element)) { element = document.getElementById(element); }

    if (lsjs.css.exists(element, class1)) {
        lsjs.css.remove(element, class1);
        lsjs.css.add(element, class2);
        return true;
    } else {
        return false;
    }
};

lsjs.css.toggle = function(element, nameOfClass) {

	if (lsjs.isString(element)) { element = document.getElementById(element); }

    if (lsjs.css.exists(element, nameOfClass)) {
    	lsjs.css.remove(element, nameOfClass);
    } else {
    	lsjs.css.add(element, nameOfClass);
    }

    return true;
};

/**
 * Creates new css.
 * 
 * Warning!!!
 * extremally slow in big document
 */
lsjs.css.create = function (rules) {	
	
	var _getStyleElement = function () {
		
		var style = document.getElementById('lsjs_styles');
		if(!style) {
			style = document.createElement('style');
			style.type = "text/css"
			style.id = 'lsjs_styles';	
		    document.getElementsByTagName('head')[0].appendChild(style);
		}
		return style;
	};
	
	var _style = _getStyleElement();	
	
	var _createRule = function(style, rule) {
		
		if(lsjs.isIE()) {
			style.styleSheet.cssText += rule;
		} else {
			style.sheet.insertRule(rule, style.sheet.cssRules.length);
		}
		
	};

	if(lsjs.isArray(rules) || (lsjs.isObject(rules) && rules.length)) {
		for(var i = 0; i < rules.length; i++) {
			_createRule(_style, rules[i]);		
		}
	} else {
		_createRule(_style, rules);
	}
		
};